//
//  MyAccountTableViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/16.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "MyAccountTableViewController.h"
#import "AccountHeaderView.h"
#import "BindingPhoneTableViewController.h"
#import "LogInPassWordTableViewController.h"
#import "AlterPayPasswordTableViewController.h"
#import "BindingOilCardTableViewController.h"
#import "BindingBankCardTableViewController.h"
#import "MyAccountCell.h"
#import "homeCollectionViewCell.h"

#import "GoodListsModel.h"
#import "ShopDetailsController.h"
#import "MyAccountModel.h"
#import "AddNameAndCardTableViewController.h"
#import "BindingAliPayTableViewController.h"

@interface MyAccountTableViewController ()

@property (nonatomic,strong)NSDictionary *userInfoDic;

@end

@implementation MyAccountTableViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:@{@"username":USERNAME} AndMenthodName:@"api/User/info" completeBlock:^(id object) {
            if (JudgeBackDataCode) {
                self.userInfoDic = [object objectForKey:@"data"];
                NSLog(@"%@",self.userInfoDic);
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            }
        }];
    });
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.backgroundColor = BACKGROUND_COLOR;
    self.title = @"个人资料";
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -25, 0, 0)];
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return 7;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    } else {
        return 10;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = FONT(15);
        cell.detailTextLabel.font = FONT(15);
    }
    
    if (!self.userInfoDic.count) {
       cell.detailTextLabel.text = @"";
    }
    
    if (indexPath.section == 0) {
        cell.textLabel.text = @"真实姓名";
        cell.textLabel.textColor = [UIColor redColor];
        
        if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"xingming"]].length) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"xingming"]];
        } else {
            cell.detailTextLabel.text = @"立即认证(必选)";
            cell.detailTextLabel.textColor = [UIColor redColor];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    } else {
        switch (indexPath.row) {
            case 0:
                cell.textLabel.text = @"身份证号";
                cell.textLabel.textColor = [UIColor redColor];
                if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"shenfen"]].length) {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"shenfen"]];
                    cell.detailTextLabel.text = [self stringForBankNumber:[self.userInfoDic objectForKey:@"shenfen"]];
                } else {
                    cell.detailTextLabel.text = @"立即认证(必选)";
                    cell.detailTextLabel.textColor = [UIColor redColor];
                }
                break;
                
            case 1:
                cell.textLabel.text = @"手机号";
                if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"phone"]].length) {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"phone"]];
                } else {
                    cell.detailTextLabel.text = @"立即认证(必选)";
                    cell.detailTextLabel.textColor = [UIColor redColor];
                }
                break;
                
            case 2:
                cell.textLabel.text = @"银行卡";
                if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"zhanghao"]].length) {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"zhanghao"]];
                    cell.detailTextLabel.text = [self stringForBankNumber:[self.userInfoDic objectForKey:@"zhanghao"]];
                } else {
                    cell.detailTextLabel.text = @"立即认证(可选)";
                    cell.detailTextLabel.textColor = [UIColor redColor];
                }
                break;
                
            case 3:
                cell.textLabel.text = @"绑定支付宝";
                if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"alipay_account"]].length) {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"alipay_account"]];
                } else {
                    cell.detailTextLabel.text = @"立即认证(可选)";
                    cell.detailTextLabel.textColor = [UIColor redColor];
                }
                break;
                
            case 4:
                cell.textLabel.text = @"油卡";
                if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"youka"]].length) {
                    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",self.userInfoDic[@"youka"]];
                } else {
                    cell.detailTextLabel.text = @"立即认证(可选)";
                    cell.detailTextLabel.textColor = [UIColor redColor];
                }
                NSLog(@"%@",cell.detailTextLabel.text);
                break;
                
            default:
                
                if (indexPath.row == 5) {
                    cell.textLabel.text = @"登录密码更改";
                } else {
                    cell.textLabel.text = @"支付密码更改";
                }
                
                break;
        }
        
        if ([cell.detailTextLabel.text isEqualToString:@"立即认证(必选)"]||[cell.detailTextLabel.text isEqualToString:@"立即认证(可选)"]) {
            cell.detailTextLabel.textColor = [UIColor redColor];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        } else {
            cell.detailTextLabel.textColor = [UIColor darkGrayColor];
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if ([NSString stringWithFormat:@"%@",self.userInfoDic[@"xingming"]].length) {
            return;
        } else {
            [self.navigationController pushForm:self.navigationController pushToAndHidden:[AddNameAndCardTableViewController new]];
        }
    } else {
        
        NSString *string;
        
        switch (indexPath.row) {
            case 0:
                string = @"shenfen";
                if ([NSString stringWithFormat:@"%@",[self.userInfoDic objectForKey:string]].length) {
                    return;
                } else {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[AddNameAndCardTableViewController new]];
                }
                break;
                
            case 1:
                string = @"phone";
                if ([NSString stringWithFormat:@"%@",[self.userInfoDic objectForKey:string]].length) {
                    return;
                } else {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[BindingPhoneTableViewController new]];
                }
                break;
                
            case 2:
                string = @"zhanghao";
                if ([NSString stringWithFormat:@"%@",[self.userInfoDic objectForKey:string]].length) {
                    return;
                } else {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[BindingBankCardTableViewController new]];
                }
                break;
                
            case 3:
                string = @"alipay_account";
                if ([NSString stringWithFormat:@"%@",[self.userInfoDic objectForKey:string]].length) {
                    return;
                } else {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[BindingAliPayTableViewController new]];
                }
                break;
            
            case 4:
                string = @"youka";
                if ([NSString stringWithFormat:@"%@",[self.userInfoDic objectForKey:string]].length) {
                    return;
                } else {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[BindingOilCardTableViewController new]];
                }
                break;
                
            default:
                
                if (indexPath.row == 5) {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[LogInPassWordTableViewController new]];
                } else {
                    [self.navigationController pushForm:self.navigationController pushToAndHidden:[AlterPayPasswordTableViewController new]];
                }
                
                break;
        }
        
    }
}

- (NSString *)stringForBankNumber:(NSString *)string{
    NSInteger length = string.length;
    NSString *firstString = [string substringWithRange:NSMakeRange(length - 4, 4)];
    NSLog(@"%@",firstString);
    
    return [NSString stringWithFormat:@"******%@",firstString];
}

- (NSDictionary *)userInfoDic {
    if (!_userInfoDic) {
        _userInfoDic = [NSDictionary dictionary];
    }
    return _userInfoDic;
}

@end
